//
//  TPPlayerManager.m
//  TPHelper
//
//  Created by xuqiang on 2020/7/28.
//  Copyright © 2020 xuqiang. All rights reserved.
//

#import "TPPlayerManager.h"
#import "MRDLNA.h"
#import <GCDWebServer/GCDWebDAVServer.h>
#import "GCDWebServerDataResponse.h"
#import  <GCDWebServer/GCDWebServerFileResponse.h>
#import "MRDLNA.h"
#import <SVProgressHUD.h>

static TPPlayerManager *manager = nil;

@interface TPPlayerManager ()<DLNADelegate,GCDWebDAVServerDelegate>
@property (nonatomic,strong)MRDLNA  *dlnaManager;
@property (nonatomic, strong) GCDWebDAVServer* davServer;
@property (nonatomic,strong)NSString  *currentStr;
@property (nonatomic,strong) NSTimer *progressTimer;
@end
@implementation TPPlayerManager

+ (instancetype)sharedTPPlayerManager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (manager == nil) {
            manager = [TPPlayerManager new];
//            [manager makeServer];
        }
    });
    return manager;
}

- (instancetype)init {
    if (self = [super init]) {
        
    }
    return self;
}

//开启服务器
-(void)makeServer{
    if (self.davServer.isRunning) {
        [self.davServer stop];
    }
    __weak typeof(self)  weakSelf = self;
    NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    self.davServer = [[GCDWebDAVServer alloc] initWithUploadDirectory:documentsPath];
    self.davServer.delegate =  self;
    [self.davServer addHandlerForMethod:@"GET" pathRegex:@"video2" requestClass:[GCDWebServerRequest class] asyncProcessBlock:^(__kindof GCDWebServerRequest * _Nonnull request, GCDWebServerCompletionBlock  _Nonnull completionBlock) {
        GCDWebServerFileResponse *res = [GCDWebServerFileResponse responseWithFile:weakSelf.currentStr byteRange:request.byteRange];
        completionBlock(res);
    }];
    [self.davServer start];
}

-(void)webServerDidStart:(GCDWebServer *)server{
    self.dlnaManager = [MRDLNA sharedMRDLNAManager];
    self.dlnaManager.delegate = self;
    self.dlnaManager.typeStr = @"2";
    self.dlnaManager.playUrl = [NSString stringWithFormat:@"%@video2",self.davServer.serverURL.absoluteString];
    [self.dlnaManager startDLNA];
//    [self.dlnaManager playTheURL:self.dlnaManager.playUrl];
}
- (void)dlnaStartPlay {
    NSLog(@"开始播放");
}

#pragma mark - Events
- (void)play {
    [self makeServer];
    if (manager.localFiles.count > 0) {
        manager.currentStr = manager.localFiles[0][@"url"];
    }
    [self.dlnaManager startDLNA];
}
- (void)start {
    [self.dlnaManager dlnaPlay];
}
- (void)pause {
    [self.dlnaManager dlnaPause];
}

- (void)stop {
    [self.dlnaManager endDLNA];
    [MRDLNA destory];
}
- (void)endDLNA {
//    [self.dlnaManager endDLNA];
    if (self.davServer.running) {
        [self.davServer stop];
    }
}
- (void)volumeAdd {
    [self.dlnaManager volumeJumpValue:5];
}
- (void)volumeLess {
    [self.dlnaManager volumeJumpValue:-5];
}
- (void)destroy {
    [self.dlnaManager endDLNA];
}
@end
